package com.lhkj.ct.base.model.ro;

import com.lhkj.ct.base.enums.ReturnStatus;

import java.io.Serializable;

/**
 * @Description: 统一返回对象
 * @Author:
 * @Date: 2018/7/20 9:19
 */
public class ActionResult<T> implements Serializable {

    /**
     * 错误码.无错误(成功)
     */
    public static final int ERR_CODE_NONE = 200;

    /**
     * 服务执行状态
     * true: 成功, code = "0"
     * false: 失败 ,  code != "0"
     */
    private boolean success = true;

    /**
     * 错误码 【小于0框架层使用，大于等于0应用层使用】
     * 0: 成功
     * 非0: 失败
     */
    protected int code = ERR_CODE_NONE;

    /**
     * 提示消息
     */
    protected String message;

    /**
     * 服务响应的业务数据
     */
    protected T data;

    /**
     * 结果生成时间
     */
    private long timestamp = System.currentTimeMillis();

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * 请求成功结果
     * @return
     */
    public static <D> ActionResult<D> success() {
        return success("操作成功", null);
    }

    /**
     * 请求成功结果
     * @return
     */
    public static <D> ActionResult<D> format(String format, Object... args) {
        return success(String.format(format, args), null);
    }

    /**
     * 请求成功结果
     * @param message 响应数据
     * @return
     */
    public static <D> ActionResult<D> success(String message) {
        return success(message, null);
    }

    /**
     * 请求成功结果
     * @param message 响应信息
     * @param data 响应数据
     * @return
     */
    public static <D> ActionResult<D> success(String message, D data) {
        ActionResult<D> actionResult = new ActionResult<>();
        actionResult.setMessage(message);
        actionResult.setData(data);
        return actionResult;
    }

    /**
     * 请求成功结果
     * @param data 响应数据
     * @return
     */
    public static <D> ActionResult<D> success(D data) {
        ActionResult<D> actionResult = new ActionResult<D>();
        actionResult.setData(data);
        return actionResult;
    }

    /**
     * 请求失败结果
     */
    public static ActionResult<Void> fail(ReturnStatus returnStatus) {
        return fail(returnStatus.getCode(), returnStatus.getDescription());
    }

    /**
     * 请求失败结果
     */
    public static <D> ActionResult<D> fail(ReturnStatus returnStatus, D data) {
        return fail(returnStatus.getCode(), data,returnStatus.getDescription());
    }

    /**
     * 请求失败结果
     * @param message 响应信息
     * @return
     */
    public static ActionResult<Void> fail() {
        return fail(400, "操作失败");
    }

    /**
     * 请求失败结果
     * @param message 响应信息
     * @return
     */
    public static ActionResult<Void> fail(String message) {
        return fail(400, message);
    }

    /**
     * 请求失败结果
     * @param code 业务代码
     * @param message 响应信息
     * @return
     */
    public static ActionResult<Void> fail(int code, String message) {
        return fail(code, null ,message);
    }

    /**
     * 请求失败结果
     * @param code 业务代码
     * @param message 响应信息
     * @return
     */
    public static <T> ActionResult<T> fail(int code, T data, String message) {
        ActionResult<T> actionResult = new ActionResult<>();
        actionResult.setCode(code);
        actionResult.setData(data);
        actionResult.setSuccess(false);
        actionResult.setMessage(message);
        return actionResult;
    }

    public static ActionResult<Void> judge(int res) {
        return judge(res > 0);
    }

    public static ActionResult<Void> judge(boolean res) {
        if (res){
            return success();
        }
        return fail("操作失败");
    }

}
